package com.gitee.wsl.ext.collection


fun <T> Collection<T>.emptyToNull() = ifEmpty { null }

/**
 * Implementation standard loop: for(int i = [start]; i < [end]; i++)
 * @param start starting index
 * @param end ending index
 */
 inline fun loop(start: Int, end: Int, action: (index: Int) -> Unit) {
    var index = start
    while (index < end) {
        action(index)
        ++index
    }
}

/**
 * Implementation standard loop: for(int i = [start]; i < [end]; i += [step])
 * @param start starting index
 * @param end ending index
 * @param step step iteration
 */
 inline fun loop(start: Int, end: Int, step: Int, action: (index: Int) -> Unit) {
    var index = start
    while (index < end) {
        action(index)
        index += step
    }
}

/**
 * 为各种 foreach 立 break flag
 *         loop{
 *            (1..10).forEach{
 *                println()
 *                print(it)
 *                if( it == 8 ) return@loop //break
 *                if( it == 5)  return@forEach //continue
 *                print("-it")
 *                println()
 *            }
 *        }
 */
inline fun loop(loopCallback:()->Unit){
    loopCallback()
}


/**
 * Calls the [selector] function for each value in the collection and returns the average of the produced values.
 */
 fun <T> Collection<T>.averageOf(selector: (T) -> Float): Float =
    fold(0f) { sum, element ->
        sum + selector(element)
    } / size

fun <T> Collection<T>.println(isDebug:Boolean=true,split:Int=5,transform:((T)->String)?=null){
    if(isDebug)
       this.windowed(split).forEach {
          println(it.joinToString(transform = transform))
       }
}

